﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xpom_Xpum__Integrated_Development_Environment.Model;
using Xpom_Xpum__Integrated_Development_Environment.Model.Controls;
using Xpom_Xpum__Integrated_Development_Environment.ViewModel.Controls;
using Xpom_Xpum__Integrated_Development_Environment.Data.Configuration;

namespace Xpom_Xpum__Integrated_Development_Environment.View.Controls
{
    /// <summary>
    /// Interaction logic for MenuView.xaml
    /// </summary>
    public partial class MenuView : UserControl
    {
        #region Constructor

        public MenuView()
        {
            InitializeComponent();
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
            {
            }
            else
            {
                this.DataContext = ((App)App.Current).mvm;
            }
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        #endregion

        #region Properties

        #endregion

        #region Methods

        /// <summary>
        /// Google's Chrome Extensions Documentation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://developer.chrome.com/extensions");
        }

        /// <summary>
        /// About
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var aw = new AboutWindowView();
            aw.Owner = ((App)App.Current).MainWindow;
            aw.ShowDialog();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// Google's Chrome Extensions Samples
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://developer.chrome.com/extensions/samples");
        }

        /// <summary>
        /// Xpom-Xpum! Local Documentation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(GlobalSettings.SDKPath + "Documentation.html");
        }

        /// <summary>
        /// Run Settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            var iswv = new IntegratorSettingsWindowView();
            iswv.Owner = ((App)App.Current).MainWindow;
            iswv.ShowDialog();
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_6(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.SaveCurrent();
        }

        /// <summary>
        /// Save As
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_7(object sender, RoutedEventArgs e)
        {
            var sfd = new System.Windows.Forms.SaveFileDialog();

            var result = sfd.ShowDialog(((App)App.Current).MainWindow as System.Windows.Forms.IWin32Window);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = sfd.FileName;

                ((App)App.Current).tcvwm.SaveCurrentAs(filePath);
            }
        }

        /// <summary>
        /// Save All
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_8(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.SaveAll();
        }

        /// <summary>
        /// Save Project As
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /*private void MenuItem_Click_9(object sender, RoutedEventArgs e)
        {
            var fbd = new System.Windows.Forms.FolderBrowserDialog();

            var result = fbd.ShowDialog(((App)App.Current).MainWindow as System.Windows.Forms.IWin32Window);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string projectPath = fbd.SelectedPath;

                ((App)App.Current).tcvwm.SaveAllAs(projectPath);
            }
        }*/

        /// <summary>
        /// CloseCurrent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_10(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.CloseCurrent();
        }

        /// <summary>
        /// CloseAll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_11(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.CloseAll();
        }

        /// <summary>
        /// Exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_12(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).MainWindow.Close();
        }

        /// <summary>
        /// Undo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_13(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Undo();
        }

        /// <summary>
        /// Redo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_14(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Redo();
        }

        /// <summary>
        /// Cut
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_15(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Cut();
        }

        /// <summary>
        /// Copy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_16(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Copy();
        }

        /// <summary>
        /// Paste
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_17(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Paste();
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_18(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.Delete();
        }

        /// <summary>
        /// Select All
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_19(object sender, RoutedEventArgs e)
        {
            ((App)App.Current).tcvwm.SelectAll();
        }

        /// <summary>
        /// Xpom-Xpum! Local Samples Library
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_20(object sender, RoutedEventArgs e)
        {
            Process.Start("Samples");
        }

        /// <summary>
        /// Find Text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_21(object sender, RoutedEventArgs e)
        {
            var frw = new FindReplaceWindowView();
            frw.Owner = ((App)App.Current).MainWindow;
            frw.Show();
        }

        /// <summary>
        /// Remove Selected Item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_22(object sender, RoutedEventArgs e)
        {
            var selectedItem = ((App)App.Current).pevm.SelectedItem;

            if ((selectedItem != ((App)App.Current).pevm.Items[0]) &&
                (selectedItem != null))
            {
                var dialogResult =
                    MessageBox.Show(((App)App.Current).MainWindow,
                    "\"" + selectedItem.Name + "\" will be deleted permanently. Do you agree?",
                    "Question", MessageBoxButton.YesNo, MessageBoxImage.Question);

                if (dialogResult == MessageBoxResult.Yes)
                {
                    if (selectedItem.IsDirectory)
                    {
                        Directory.Delete(selectedItem.Path, true);
                    }
                    else
                    {
                        File.Delete(selectedItem.Path);
                        ((App)App.Current).tcvwm.Close(selectedItem.Path);
                    }

                    ((App)App.Current).pevm = new ViewModel.Controls.ProjectExplorerViewModel(((App)App.Current).pevm.ProjectDirectory);
                }
            }
        }

        /// <summary>
        /// Add Existing Item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_23(object sender, RoutedEventArgs e)
        {
            var ofd = new System.Windows.Forms.OpenFileDialog();

            ofd.Filter = "HTML, CSS, Script, PNG, ICO, TXT, Manifest Files (*.html, *.htm, *.css, *.png, *.ico, *.txt, manifest.json)|*.html;*.htm;*.css;*.png;*.ico;*.txt;manifest.json|" +
                "HTML Files (*.html, *.htm)|*.html;*.htm|" +
                "CSS Files (*.css)|*.css|" +
                "Script Files (*.js)|*.js|" +
                "PNG Files (*.png)|*.png|" +
                "Icon Files (*.ico)|*.ico|" +
                "Text Files (*.txt)|*.txt|" +
                "Manifest Files (manifest.json)|manifest.json|" +
                "All Files (*.*)|*.*";

            var result = ofd.ShowDialog(((App)App.Current).MainWindow as System.Windows.Forms.IWin32Window);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                ProjectItemModel parent;

                var selectedItem = ((App)App.Current).pevm.SelectedItem;

                if (selectedItem != null)
                {
                    if (selectedItem.IsDirectory)
                    {
                        parent = ((App)App.Current).pevm.SelectedItem;
                    }
                    else
                    {
                        parent = ((App)App.Current).pevm.Items[0];
                    }
                }
                else
                {
                    parent = ((App)App.Current).pevm.Items[0];
                }

                //

                var newItemPath = parent.Path + "\\" + System.IO.Path.GetFileName(ofd.FileName);

                File.Copy(ofd.FileName, newItemPath);

                //

                ((App)App.Current).pevm = new ViewModel.Controls.ProjectExplorerViewModel(((App)App.Current).pevm.ProjectDirectory);
            }
        }

        /// <summary>
        /// Pack To ZIP
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_24(object sender, RoutedEventArgs e)
        {
            var sfd = new System.Windows.Forms.SaveFileDialog();

            sfd.InitialDirectory = Directory.GetParent(((App)App.Current).pevm.ProjectDirectory).FullName;
            sfd.DefaultExt = "zip";
            sfd.Filter = "ZIP Files (*.zip)|*.zip|All Files (*.*)|*.*";
            sfd.FileName = ((App)App.Current).pevm.ProjectName + ".zip";

            var result = sfd.ShowDialog(((App)App.Current).MainWindow as System.Windows.Forms.IWin32Window);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string zipFilePath = sfd.FileName;

                var ZIPProcess = new Process();
                ZIPProcess.StartInfo.Arguments =
                    "-zip \"" + ((App)App.Current).pevm.ProjectDirectory.Replace("\\", "/") +
                    "\" \"" + zipFilePath.Replace("\\", "/");
                //ZIPProcess.StartInfo.CreateNoWindow = true;
                ZIPProcess.StartInfo.FileName = GlobalSettings.SDKPath + "Environment/Xpom-Xpum! Browser Integrator/xbint.exe";
                ZIPProcess.StartInfo.UseShellExecute = false;
                ZIPProcess.Start();
            }
        }

        /// <summary>
        /// Recent Files List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_25(object sender, RoutedEventArgs e)
        {
            MenuItem obMenuItem = e.OriginalSource as MenuItem;

            string projectPath = obMenuItem.Header.ToString();

            if (Directory.Exists(projectPath))
            {
                ((App)App.Current).pevm = new ProjectExplorerViewModel(projectPath);
            }
            else
            {
                MessageBox.Show("Project at path \"" + projectPath + "\" not found.",
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ShowNewItemDialog(ItemTemplate selectedItemTemplate)
        {
            var niwv = ((App)App.Current).niwv;
            var MainWindow = Application.Current.MainWindow;
            niwv.Owner = MainWindow;
            ((App)App.Current).niwvm.SelectedItem = selectedItemTemplate;
            if (MainWindow.WindowState == WindowState.Normal)
            {
                niwv.Left = MainWindow.Left + (MainWindow.Width - niwv.Width) / 2;
                niwv.Top = MainWindow.Top + (MainWindow.Height - niwv.Height) / 2;
            }
            else
            {
                var workArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
                niwv.Left = workArea.Left + (workArea.Width - niwv.Width) / 2;
                niwv.Top = workArea.Top + (workArea.Height - niwv.Height) / 2;
            }
            niwv.ShowDialog();
        }

        /// <summary>
        /// Add New Item...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            ShowNewItemDialog(((App)App.Current).niwvm.ItemTemplates[0]);
        }

        /// <summary>
        /// Add Script...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_9(object sender, RoutedEventArgs e)
        {
            ShowNewItemDialog(((App)App.Current).niwvm.ScriptItemTemplate);
        }

        /// <summary>
        /// Add HTML File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_26(object sender, RoutedEventArgs e)
        {
            ShowNewItemDialog(((App)App.Current).niwvm.HTMLItemTemplate);
        }

        /// <summary>
        /// Add CSS Style
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_27(object sender, RoutedEventArgs e)
        {
            ShowNewItemDialog(((App)App.Current).niwvm.CSSItemTemplate);
        }

        /// <summary>
        /// Add New Folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_28(object sender, RoutedEventArgs e)
        {
            ShowNewItemDialog(((App)App.Current).niwvm.FolderItemTemplate);
        }

        /// <summary>
        /// Run With No Debug
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_29(object sender, RoutedEventArgs e)
        {
            string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
            string programFilesx64 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");

            string browserExePath = ((App)App.Current).iswvm.Browsers[((App)App.Current).iswvm.SelectedBrowserIndex].BrowserExecutablePath;
            string browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
            programFiles);
            if (!File.Exists(browserExePathFinal))
            {
                browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
                programFilesx64);
            }

            var CRXProcess = new Process();
            CRXProcess.StartInfo.Arguments =
                "-run \"" + browserExePathFinal.Replace("\\", "/") +
                "\" \"" + ((App)App.Current).pevm.ProjectDirectory.Replace("\\", "/");
            //ZIPProcess.StartInfo.CreateNoWindow = true;
            CRXProcess.StartInfo.FileName = GlobalSettings.SDKPath + "Environment/Xpom-Xpum! Browser Integrator/xbint.exe";
            CRXProcess.StartInfo.UseShellExecute = false;
            CRXProcess.Start();

        }

        /// <summary>
        /// Pack To CRX
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_30(object sender, RoutedEventArgs e)
        {
            string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
            string programFilesx64 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");

            string browserExePath = ((App)App.Current).iswvm.Browsers[((App)App.Current).iswvm.SelectedBrowserIndex].BrowserExecutablePath;
            string browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
            programFiles);
            if (!File.Exists(browserExePathFinal))
            {
                browserExePathFinal = browserExePath.Replace("%ProgramFiles%",
                programFilesx64);
            }

            var CRXProcess = new Process();
            CRXProcess.StartInfo.Arguments =
                "-crx \"" + browserExePathFinal.Replace("\\", "/") +
                "\" \"" + ((App)App.Current).pevm.ProjectDirectory.Replace("\\", "/");
            //ZIPProcess.StartInfo.CreateNoWindow = true;
            CRXProcess.StartInfo.FileName = GlobalSettings.SDKPath + "Environment/Xpom-Xpum! Browser Integrator/xbint.exe";
            CRXProcess.StartInfo.UseShellExecute = false;
            CRXProcess.Start();

        }

        #endregion
    }
}
